/****** Object:  StoredProcedure [dbo].[GetMessagesInThread]    Script Date: 10/13/2011 17:21:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetMessagesInThread]
	@ThreadID int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	SELECT
			MesTbl.ID,
			MesTbl.ForumID,
			MesTbl.name as staticUsername,
			MesTbl.subject,
			MesTbl.mesdate,
			MesTbl.thread_parent,
			MesTbl.thread_id,
			MesTbl.locked,
			MesTbl.lastreply,
			MesTbl.threadviews,
			MesTbl.score,
			MesTbl.protectedscore,
			MesTbl.pollid,
			MesTbl.userID,
			Forum_Users.username,
			Forum_Groups.groupname
		FROM MesTbl
		LEFT JOIN Forum_Users ON MesTbl.userID = Forum_Users.userID
		LEFT JOIN Forum_Groups ON Forum_Users.userlevel = Forum_Groups.levelid
		WHERE 
			MesTbl.ID = @ThreadID
		OR
			MesTbl.Thread_ID = @ThreadID
		order by mesdate desc

END

GO


